package com.bops.web.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.springframework.validation.Errors;
import org.springframework.web.multipart.MultipartFile;

import com.bops.biz.permission.GrantManager;
import com.bops.dal.product.dataobject.CloudDO;
import com.bops.dal.product.dataobject.ProductsDO;
import com.bops.dal.product.dataobject.TypeOne;
import com.bops.dal.product.dataobject.TypeThree;
import com.bops.dal.product.dataobject.TypeTwo;
import com.bops.dal.product.queryobject.CloudQuery;
import com.bops.dal.product.queryobject.ProductsQuery;
import com.bops.dal.product.queryobject.TypeOneQuery;
import com.bops.dal.product.queryobject.TypeThreeQuery;
import com.bops.dal.product.queryobject.TypeTwoQuery;
import com.bops.service.CloudService;
import com.bops.service.CommonServiceLocator;
import com.bops.service.ProductService;
import com.bops.service.SupplierService;
import com.bops.web.CheckUpload;
import com.yue.commons.log.LoggerFactoryWrapper;
import com.yue.commons.seine.web.annotations.ControllerAction;
import com.yue.commons.seine.web.servlet.mvc.AbstractController;
import com.yue.commons.seine.web.servlet.result.Forward;
import com.yue.commons.seine.web.servlet.result.View;
import com.yue.commons.seine.web.servlet.result.WebResult;

/**
 * @author xyj
 * 
 */

public class ProductController extends AbstractController {

	private ProductService productService = CommonServiceLocator
			.getProductService();
	private CloudService cloudService = CommonServiceLocator.getCloudService();
	private SupplierService supplierService = CommonServiceLocator
			.getSupplierService();
	private static Logger log = LoggerFactoryWrapper
			.getLogger(ProductController.class);

	/**
	 * 购买产品
	 * */
	@ControllerAction
	public WebResult buy(Map<String, Object> model,Integer id){
		ProductsDO productDO = new ProductsDO();
		if (id == null) {
			model.put("errorStatus", "产品不存在!");
			return null;
		}
		productDO = productService.findProductById(id);
		if (productDO == null) {
			model.put("errorStatus", "该产品不存在，或已下架!");
			return null;
		}
		model.put("productDO", productDO);
		return new View("/productCenter/item.htm");
	}
	
	/**
	 * 行业应用云
	 * */
	@ControllerAction
	public WebResult industry(Map<String, Object> model,ProductsQuery queryProduct,String action){
		queryProduct.setTypeThreeName("行业应用云");
		queryProduct.setPageSize(12);
		products(model, queryProduct);
		return new View("/productCenter/industry.vm");
	}
	
	/**
	 * 基础架构云
	 * */
	@ControllerAction
	public WebResult base(Map<String, Object> model,ProductsQuery queryProduct,String action){
		queryProduct.setTypeThreeName("基础架构云");
		queryProduct.setPageSize(12);
		products(model, queryProduct);
		return new View("/productCenter/base.vm");
	}
	
	/**
	 * 应用中心
	 * */
	@ControllerAction
	public WebResult application(Map<String, Object> model,ProductsQuery queryProduct,String action){
		queryProduct.setTypeThreeName("应用服务");
		queryProduct.setPageSize(12);
		products(model, queryProduct);
		return new View("/productCenter/application.vm");
	}
	
	/**
	 * 查询到所有产品
	 * */
	public void products(Map<String, Object> model,ProductsQuery queryProduct) {
		
		
		List<ProductsDO> productList = productService.listProduct(queryProduct);
		if (productList != null && productList.size() > 0) {
			model.put("productList", productList);
		}
		model.put("productDO", queryProduct);
		model.put("productList", productList);
		model.put("pagniation", productList);
	}

	@ControllerAction
	public WebResult deleteProduct(Map<String, Object> model, Integer id) {
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的！");
			return null;
		}
		productService.deleteProductById(id);
		model.put("actionStatus", id + "已经被删除成功!");
		return searchProductForm(model, null);
	}

	@ControllerAction
	public WebResult viewProductForm(Map<String, Object> model, Integer id,
			String action) {
		ProductsDO productDO = new ProductsDO();
		model.put("action", action);
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的!");
			return null;
		}
		productDO = productService.findProductById(id);
		if (productDO == null) {
			productDO=productService.findProduct_cloud_ById(id);
			if(productDO ==null){
			  model.put("errorStatus", "没有编号是" + id + "的记录!");
			  return null;
			}
			
		}
		model.put("productDO", productDO);
		if(productDO.getCloud_id() !=null){
			CloudDO cloudDO=cloudService.getById(productDO.getCloud_id());
			model.put("cloudDO", cloudDO);
			return new View("/product/viewProduct1.vm");
		}
		return new View("/product/viewProduct.htm");
	}

	@ControllerAction
	public WebResult searchProductForm(Map<String, Object> model,
			ProductsQuery queryProduct) {
		if (GrantManager.hasPermission("product_add_role")) {
			model.put("ADD_PRODUCT", "true");
		}

		if (queryProduct == null) {
			queryProduct = new ProductsQuery();
			queryProduct.setNowPageIndex(0);
		}
		List<ProductsDO> productList = productService.listProduct(queryProduct);
		model.put("productDO", queryProduct);
		if (productList != null && productList.size() > 0) {
			model.put("productList", productList);
		}
		model.put("pagniation", productList);
		model.put("supplierList", supplierService.listSupplier(null));
		model.put("type3List", productService.listType3(null));
		return new View("/product/searchProduct.htm");

	}

	@ControllerAction
	public WebResult searchProduct(Map<String, Object> model) {
		model.put("notShowRecord", true);
		model.put("supplierList", supplierService.listSupplier(null));
		model.put("type3List", productService.listType3(null));
		return new View("/product/searchProduct.htm");
	}

	@ControllerAction
	public WebResult editProductForm(Map<String, Object> model,
			ProductsDO editProduct, String action, Errors e, MultipartFile file)
			throws Exception {

		if ("add".equals(action)) {
			String msg = CheckUpload.checkImg(file, "c://");
			if (!"success".equals(msg)) {
				model.put("actionStatus", msg);
				return new Forward("/product/editProduct.htm");
			}
			ProductsQuery query = new ProductsQuery();
			query.setImage(file.getOriginalFilename());
			List<ProductsDO> productList = productService.listProduct(query);
			if (productList.size() > 0) {
				model.put("actionStatus", "存在相同的图片名字");
				return new Forward("/product/editProduct.htm");
			}
			CheckUpload.imageUpload(file, "c://");
			editProduct.setImage(file.getOriginalFilename());
			productService.insertProduct(editProduct);
			model.put("actionStatus", "添加成功!请继续添加!");
			return new Forward("/product/editProduct.htm");
		} else {
			productService.updateProduct(editProduct);
			model.put("actionStatus", "修改成功!");
			return new Forward("/product/editProduct.htm?action=" + action
					+ "&id=" + editProduct.getId());
		}
	}

	@ControllerAction
	public WebResult editProduct(Map<String, Object> model, Integer id,
			String action) {
		ProductsDO productDO = new ProductsDO();
		if ("edit".equals(action) || "view".equals(action)) {
			model.put("action", action);
			if (id == null) {
				model.put("errorStatus", "编号是" + id + "记录是错误的!");
				return null;
			}
			productDO = productService.findProductById(id);
			if (productDO == null) {
				model.put("errorStatus", "没有编号是" + id + "的记录!");
				return null;
			}
		} else {
			model.put("action", "add");
		}
		model.put("supplierList", supplierService.listSupplier(null));
		model.put("type3List", productService.listType3(null));
		model.put("productDO", productDO);
		return new View("/product/editProduct.htm");
	}

	@ControllerAction
	public WebResult deleteCloud(Map<String, Object> model, Integer id) {
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的！");
			return null;
		}
		cloudService.delete(id);
		model.put("actionStatus", id + "已经被删除成功!");
		return searchCloudForm(model, null);
	}

	@ControllerAction
	public WebResult searchCloudForm(Map<String, Object> model,
			CloudQuery queryCloud) {
		if (GrantManager.hasPermission("cloud_add_role")) {
			model.put("ADD_CLOUD", "true");
		}

		if (queryCloud == null) {
			queryCloud = new CloudQuery();
			queryCloud.setNowPageIndex(0);
		}
		List<CloudDO> cloudList = cloudService.listCloud(queryCloud);
		model.put("cloudDO", queryCloud);
		if (cloudList != null && cloudList.size() > 0) {
			model.put("cloudList", cloudList);
		}
		model.put("pagniation", cloudList);

		return new View("/product/searchCloud.htm");

	}

	@ControllerAction
	public WebResult editCloudForm(Map<String, Object> model,
			CloudDO editCloud, String action, Errors e) {

		if ("add".equals(action)) {
			Integer id = cloudService.insert(editCloud);
			model.put("actionStatus", "添加成功!请继续添加!");
			return new Forward("/product/editCloud.htm");
		} else {
			cloudService.update(editCloud);
			model.put("actionStatus", "修改成功!");
			return new Forward("/product/editCloud.htm?action=" + action
					+ "&id=" + editCloud.getId());
		}
	}

	@ControllerAction
	public WebResult searchCloud(Map<String, Object> model) {
		model.put("notShowRecord", true);
		return new View("/product/searchCloud.htm");
	}

	@ControllerAction
	public WebResult editCloud(Map<String, Object> model, Integer id,
			String action) {
		CloudDO cloudDO = new CloudDO();
		if ("edit".equals(action) || "view".equals(action)) {
			model.put("action", action);
			if (id == null) {
				model.put("errorStatus", "编号是" + id + "记录是错误的!");
				return null;
			}
			cloudDO = cloudService.getById(id);
			if (cloudDO == null) {
				model.put("errorStatus", "没有编号是" + id + "的记录!");
				return null;
			}
		} else {
			model.put("action", "add");
		}
		model.put("cloudList", cloudService.listCloud(null));
		model.put("cloudDO", cloudDO);
		return new View("/product/editCloud.htm");
	}

	@ControllerAction
	public WebResult searchType3(Map<String, Object> model) {
		model.put("type2List", productService.listType2(null));
		model.put("notShowRecord", true);
		return new View("/product/searchType3.htm");
	}

	@ControllerAction
	public WebResult searchType3Form(Map<String, Object> model,
			TypeThreeQuery queryType3) {

		if (GrantManager.hasPermission("product_add_role")) {
			model.put("ADD_TYPE_3", "true");
		}

		if (queryType3 == null) {
			queryType3 = new TypeThreeQuery();
			queryType3.setNowPageIndex(0);
		}
		List<TypeThree> type3List = productService.listType3(queryType3);
		model.put("type3DO", type3List);
		if (type3List != null && type3List.size() > 0) {
			model.put("type3List", type3List);
		}
		model.put("pagniation", type3List);
		model.put("type2List", productService.listType2(null));

		return new View("/product/searchType3.htm");
	}

	@ControllerAction
	public WebResult deleteType3(Map<String, Object> model, Integer id) {
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的！");
			return null;
		}
		productService.deleteType3ById(id);
		model.put("actionStatus", id + "已经被删除成功!");
		return searchType3Form(model, null);
	}

	@ControllerAction
	public WebResult editType3Form(Map<String, Object> model,
			TypeThree editType3, String action, Errors e) {

		String name = editType3.getVersion();
		if (name != null && !"".equals(name)) {
			int count = productService.checkType3Name(editType3);
			if (count > 0) {// 名称不能重复
				model.put("nameMessage", "已经存在相同的产品型号，请修改！ 型号=" + name);
				model.put("type3DO", editType3);
				model.put("action", action);
				model.put("type2List", productService.listType2(null));
				return new View("/product/editType3.htm");
			}
		}
		if ("add".equals(action)) {
			productService.insertType3(editType3);
			model.put("actionStatus", "添加成功!请继续添加!");
			return new Forward("/product/editType3.htm");
		} else {
			productService.updateType3(editType3);
			model.put("actionStatus", "修改成功!");
			return new Forward("/product/editType3.htm?action=" + action
					+ "&id=" + editType3.getId());
		}
	}

	@ControllerAction
	public WebResult editType3(Map<String, Object> model, Integer id,
			String action) {
		TypeThree type3DO = new TypeThree();
		if ("edit".equals(action) || "view".equals(action)) {
			model.put("action", action);
			if (id == null) {
				model.put("errorStatus", "编号是" + id + "记录是错误的!");
				return null;
			}
			type3DO = productService.findType3ById(id);
			if (type3DO == null) {
				model.put("errorStatus", "没有编号是" + id + "的小类记录!");
				return null;
			}
		} else {
			model.put("action", "add");
		}
		model.put("type2List", productService.listType2(null));
		model.put("type3DO", type3DO);
		return new View("/product/editType3.htm");
	}

	@ControllerAction
	public WebResult searchType2(Map<String, Object> model) {
		model.put("type1List", productService.listType1(null));
		model.put("notShowRecord", true);
		return new View("/product/searchType2.htm");
	}

	@ControllerAction
	public WebResult searchType2Form(Map<String, Object> model,
			TypeTwoQuery queryType2) {

		if (GrantManager.hasPermission("product_add_role")) {
			model.put("ADD_TYPE_2", "true");
		}

		if (queryType2 == null) {
			queryType2 = new TypeTwoQuery();
			queryType2.setNowPageIndex(0);
		}
		List<TypeTwo> type2List = productService.listType2(queryType2);
		model.put("type2DO", queryType2);
		if (type2List != null && type2List.size() > 0) {
			model.put("type2List", type2List);
		}
		model.put("pagniation", type2List);
		model.put("type1List", productService.listType1(null));

		return new View("/product/searchType2.htm");
	}

	@ControllerAction
	public WebResult deleteType2(Map<String, Object> model, Integer id) {
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的！");
			return null;
		}
		productService.deleteType2ById(id);
		model.put("actionStatus", id + "已经被删除成功!");
		return searchType2Form(model, null);
	}

	@ControllerAction
	public WebResult editType2Form(Map<String, Object> model,
			TypeTwo editType2, String action, Errors e) {

		String name = editType2.getName();
		if (name != null && !"".equals(name)) {// 存在的话再判断该大类里是不是存在该名称的小类
			int count = productService.checkType2Name(editType2);
			if (count > 0) {// 名称不能重复
				model.put("nameMessage", "已经存在相同的名称小类，请修改！ 小类=" + name);
				model.put("type2DO", editType2);
				model.put("action", action);
				model.put("type1List", productService.listType1(null));
				return new View("/product/editType2.htm");
			}
		}
		if ("add".equals(action)) {
			productService.insertType2(editType2);
			model.put("actionStatus", "添加成功!请继续添加!");
			return new Forward("/product/editType2.htm");
		} else {
			productService.updateType2(editType2);
			model.put("actionStatus", "修改成功!");
			return new Forward("/product/editType2.htm?action=" + action
					+ "&id=" + editType2.getId());
		}
	}

	@ControllerAction
	public WebResult editType2(Map<String, Object> model, Integer id,
			String action) {
		TypeTwo type2DO = new TypeTwo();
		if ("edit".equals(action) || "view".equals(action)) {
			model.put("action", action);
			if (id == null) {
				model.put("errorStatus", "编号是" + id + "记录是错误的!");
				return null;
			}
			type2DO = productService.findType2ById(id);
			if (type2DO == null) {
				model.put("errorStatus", "没有编号是" + id + "的小类记录!");
				return null;
			}
		} else {
			model.put("action", "add");
		}
		model.put("type1List", productService.listType1(null));
		model.put("type2DO", type2DO);
		return new View("/product/editType2.htm");
	}

	@ControllerAction
	public WebResult deleteType1(Map<String, Object> model, Integer id) {
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的！");
			return null;
		}
		productService.deleteType1ById(id);
		model.put("actionStatus", id + "已经被删除成功!");
		return searchType1Form(model, null);
	}

	@ControllerAction
	public WebResult editERPType1(Map<String, Object> model, Integer id,
			String action) {
		TypeOne editType1 = new TypeOne();
		if ("edit".equals(action) || "view".equals(action)) {
			model.put("action", action);
			if (id == null) {
				model.put("errorStatus", "编号是" + id + "记录是错误的！");
				return null;
			}
			editType1 = productService.findType1ById(id);
			if (editType1 == null) {
				model.put("errorStatus", "没有编号是" + id + "的大类记录！");
				return null;
			}
		} else {
			model.put("action", "add");
		}
		model.put("editType1", editType1);
		return new View("/product/editType1.htm");
	}

	@ControllerAction
	public WebResult editType1Form(Map<String, Object> model,
			TypeOne editType1, String action, Errors e) {
		String name = editType1.getName();
		if (name != null && !"".equals(name)) {
			int count = productService.checkType1Name(editType1);
			if (count > 0) {// 名称不能重复
				model.put("nameMessage", "已经存在相同的名称.");
				model.put("editType1", editType1);
				model.put("action", action);
				return new View("/product/editType1.htm");
			}
		}
		if ("add".equals(action)) {
			productService.insertType1(editType1);
			model.put("actionStatus", "添加成功!请继续添加!");
			return new Forward("/product/editType1.htm");
		} else {
			productService.updateType1(editType1);
			model.put("actionStatus", "修改成功!");
			return new Forward("/product/editType1.htm?action=" + action
					+ "&id=" + editType1.getId());
		}
	}

	@ControllerAction
	public WebResult searchType1Form(Map<String, Object> model,
			TypeOneQuery queryType1) {

		if (GrantManager.hasPermission("product_add_role")) {
			model.put("ADD_TYPE_1", "true");
		}

		if (queryType1 == null) {
			queryType1 = new TypeOneQuery();
			queryType1.setNowPageIndex(0);
		}
		List<TypeOne> type1List = productService.listType1(queryType1);
		model.put("type1DO", queryType1);
		if (type1List != null && type1List.size() > 0) {
			model.put("type1List", type1List);
		}
		model.put("pagniation", type1List);
		return new View("/product/searchType1.htm");
	}

}
